package xuanzhen.manage.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import xuanzhen.dating.pojo.DatingUser;
import xuanzhen.dating.pojo.SeeShare;
import xuanzhen.dating.pojo.SignList;
import xuanzhen.dating.service.DatingService;
import xuanzhen.dating.service.SeeShareService;
import xuanzhen.manage.pojo.Admins;
import xuanzhen.manage.service.AdminsService;
import xuanzhen.pro.pojo.Relay;
import xuanzhen.pro.service.RelayService;

@Controller
@RequestMapping("/admins")
public class AdminsController {
	@Resource
	private AdminsService adminsService;
	@Resource
	private RelayService relayService;
	@Resource
	private DatingService datingService;
	@Resource
	private SeeShareService seeshareService;
	/**
	 * 登录
	 */
	@RequestMapping(value = "/begin", method = RequestMethod.GET)
	public String to(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,Admins admins)
			throws Exception {

			return "login";
		
	}
	/**
	 * 返回指示板index
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String back(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,Admins admins)
			throws Exception {
			return "tables";
		
	}
	/**
	 * 登出
	 */
	@RequestMapping(value = "/out", method = RequestMethod.GET)
	public String out(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,Admins admins)
			throws Exception {
		session.setAttribute("admins", null);
		model.addAttribute("admins", null);
			return "login";
		
	}
	/**
	 * 登录
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String toSg(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,Admins admins)
			throws Exception {
		int tag= adminsService.login(admins);
		//判断
		if(tag==1){
			session.setAttribute("admins", admins);
			int pageCount =relayService.countRecords();
			model.addAttribute("pageCount",pageCount);
			List<Relay> relayList= relayService.selectByPageNo(0);
			model.addAttribute("relayList", relayList);
			return "tables";
		}else{
			
			return "login";
		}
		
	}
	/**
	 *接车用户 
	 */
	@RequestMapping(value = "/jielong", method = RequestMethod.GET)
	public String tojielong(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Admins admin= (Admins) session.getAttribute("admins");
		int tag= adminsService.login(admin);
		//判断
		if(tag==1){
			model.addAttribute("admins", admin);
			int pageCount =relayService.countRecords();
			model.addAttribute("pageCount",pageCount);
			List<Relay> relayList= relayService.selectByPageNo(0);
			model.addAttribute("relayList", relayList);
			return "tables";
		}else{
			
			return "login";
		}
		
	}
	
	/**
	 *接车用户 翻页
	 */
	@RequestMapping(value = "/newPage", method = RequestMethod.POST)
	@ResponseBody
	public Object toNewPage(Model model, @RequestParam("pageNo") int pageNo,HttpServletResponse response)
			throws Exception {
		System.out.println(pageNo);
		List<Relay> relayList= relayService.selectByPageNo(pageNo);
		/*JSONArray json1 = new JSONArray();
		json1.addAll(relayList);*/
		return relayList;
	}
	/**
	 * 11 11dating用户表
	 */
	@RequestMapping(value = "/dating", method = RequestMethod.GET)
	public String datingUser(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,Admins admins)
			throws Exception {
		Admins admin= (Admins) session.getAttribute("admins");
		int tag= adminsService.login(admin);
		//判断
		if(tag==1){
			model.addAttribute("admins", admin);
			int pageCount =datingService.countRecords();
			model.addAttribute("pageCount",pageCount);
			List<DatingUser> datingUserList = datingService.selectDatingUser(0);
			model.addAttribute("datingUserList", datingUserList);
			return "datingUserList";
		}else{
			
			return "login";
		}
		
	}
	/**
	 * dating用户shareList表
	 */
	@RequestMapping(value = "/datingcount", method = RequestMethod.GET)
	public String datingcount(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,Admins admins)
			throws Exception {
		Admins admin= (Admins) session.getAttribute("admins");
		int tag= adminsService.login(admin);
		//判断
		if(tag==1){
			model.addAttribute("admins", admin);
			List<SeeShare> shareList = seeshareService.selectAll();
			model.addAttribute("shareList", shareList);
			List<SignList> signList  = datingService.selectSignList();
			model.addAttribute("signList", signList);
			return "shareList";
		}else{
			
			return "login";
		}
		
	}
	/**
	 *dating用户 翻页
	 */
	@RequestMapping(value = "/datingNewPage", method = RequestMethod.POST)
	@ResponseBody
	public Object datingCounttoNewPage(Model model, @RequestParam("pageNo") int pageNo,HttpServletResponse response)
			throws Exception {
		System.out.println(pageNo);
		List<DatingUser> datingUserList= datingService.selectDatingUser(pageNo);
		/*JSONArray json1 = new JSONArray();
		json1.addAll(relayList);*/
		return datingUserList;
	}
	/**
	 *dating用户 翻页
	 */
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	@ResponseBody
	public Object search(Model model, @RequestParam("pageNo") int pageNo,@RequestParam("key") String key,HttpServletResponse response)
			throws Exception {
		System.out.println(pageNo);
		System.out.println(key);
		List<DatingUser> datingUserList= datingService.selectDatingUserBykey(pageNo,key);
		/*JSONArray json1 = new JSONArray();
		json1.addAll(relayList);*/
		return datingUserList;
	}
}
